package com.service.admin.base.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.service.admin.base.controller.rep.WalletPoolPageRep;
import com.service.admin.base.controller.req.WalletConfigReq;
import com.service.admin.base.controller.req.WalletPoolDeleteReq;
import com.service.admin.base.controller.req.WalletPoolPageReq;
import com.service.admin.base.controller.req.WalletPoolReq;
import com.service.admin.base.service.WalletPoolService;
import com.service.admin.framework.log.RequestLog;
import com.service.admin.upload.controller.req.WalletPoolUpLoadReq;
import com.service.core.exception.CommonException;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.PageResponse;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.WalletEnums;
import com.service.dao.model.WalletPool;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年12月02日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("wallet-pool")
public class WalletPoolController extends BaseController {

    @Autowired
    private WalletPoolService walletPoolService;

    @RequestLog("钱包池接口")
    @ApiOperation("分页")
    @PostMapping("page")
    public PageResponse<WalletPoolPageRep> page(@ApiParam @RequestBody WalletPoolPageReq pageReq) {
        return successQueryPageResponse(walletPoolService.page(pageReq));
    }

    @RequestLog("钱包池导入")
    @ApiOperation("分页")
    @PostMapping("importExcel/{coinName}")
    public BaseResponse importExcel(@PathVariable("coinName") CoinsEnum coinTypeEnum, @RequestParam("file") MultipartFile file) {

        ImportParams importParams = new ImportParams();
        // 数据处理
        importParams.setHeadRows(1);
        importParams.setTitleRows(0);
        // 需要验证
        importParams.setNeedVerfiy(false);
        List<WalletPoolUpLoadReq> WallPoolList = null;
        try {
            ExcelImportResult<WalletPoolUpLoadReq> result = ExcelImportUtil.importExcelMore(file.getInputStream(), WalletPoolUpLoadReq.class,
                    importParams);
            WallPoolList = result.getList();
            log.info("从Excel导入数据一共 {} 行 ", WallPoolList.size());
            walletPoolService.importFromExcel(coinTypeEnum.getCode(), WallPoolList);
            return successSaveBaseResponse();
        } catch (Exception e) {
            log.error("excel解析出错", e);
            if(e instanceof CommonException){
                throw new CommonException(((CommonException) e).getMsg());
            }
            throw new CommonException("excel解析出错");
        }
    }


    @RequestLog("钱包池创建")
    @ApiOperation("创建钱包池")
    @PostMapping("generateWallets")
    public BaseResponse generateWallets(@ApiParam @RequestBody WalletPoolReq walletPoolReq){
        walletPoolService.generateWallets(walletPoolReq);
        return successBaseResponse();
    }


    @RequestLog("批量删除")
    @PostMapping("single-delete/{walletId}")
    public BaseResponse deleteSingle(@PathVariable("walletId") Integer walletId) {
        walletPoolService.removeByIds(Lists.newArrayList(walletId));
        return successSaveBaseResponse();
    }

    @RequestLog("批量删除")
    @PostMapping("batch-delete")
    public BaseResponse deleteBatch(@RequestBody() WalletPoolDeleteReq req) {
        walletPoolService.removeByIds(req.getIds());
        return successSaveBaseResponse();
    }

    @RequestLog("批量删除")
    @PostMapping("delete-all/{walletType}/{coins}")
    public BaseResponse deleteAll(@PathVariable("coins") CoinsEnum coins,
                                  @PathVariable("walletType") WalletEnums.Type type) {
        walletPoolService.remove(new QueryWrapper<WalletPool>().lambda()
                .eq(WalletPool::getCoinsId, coins.getCode())
                .eq(WalletPool::getWalletType, type.getCode())
                .eq(WalletPool::getAddrStatus, WalletEnums.Status.ACTIVE.getCode()));
        return successSaveBaseResponse();
    }
}
